package chapter8JVM_excute;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

//java的MathodHandler进行方法的动态调用 作用于虚拟机层面的直接调用方法，方法动态调用方面的性能比反射好
public class P83_3 {
    static class Out{
        public void println(String s){
            System.out.println(s+"\n println");
        }
    }
    public static void main(String[] args) throws Throwable {
        Object o = System.currentTimeMillis()%2==0?System.out:new Out();
        getPrintlnMh(o).invoke("zhuyingzhi");

    }

    private static MethodHandle getPrintlnMh(Object recerver) throws NoSuchMethodException, IllegalAccessException {
        MethodType methodType = MethodType.methodType(void.class,String.class);
        System.out.println(recerver.getClass());
        return MethodHandles.lookup().findVirtual(recerver.getClass(),"println",methodType).bindTo(recerver);
    }
}
